package v1

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"

	bc "application/blockchain"
	"application/pkg/app"

	"github.com/gin-gonic/gin"
)

type RealEstateRequestBody struct {
	AccountId    string  `json:"accountId" form:"accountId"`     //操作人ID
	Proprietor   string  `json:"proprietor" form:"proprietor"`   //所有者(业主)(业主AccountId)
	HouseName    string  `json:"houseName" form:"houseName"`     //房屋名称
	TotalArea    float64 `json:"totalArea" form:"totalArea"`     //总面积
	LivingSpace  float64 `json:"livingSpace" form:"livingSpace"` //生活空间
	HouseRent    float64 `json:"houseRent" form:"houseRent"`
	HouseComment string  `json:"houseComment" form:"houseComment"` //房屋描述
}

type RealEstateQueryRequestBody struct {
	Proprietor string `json:"proprietor"` //所有者(业主)(业主AccountId)
}

//func CreateRealEstate(c *gin.Context) {
//	appG := app.Gin{C: c}
//	body := new(RealEstateRequestBody)
//	//解析Body参数
//	if err := c.ShouldBind(body); err != nil {
//		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("参数出错%s", err.Error()))
//		return
//	}
//	if body.TotalArea <= 0 || body.LivingSpace <= 0 || body.LivingSpace > body.TotalArea {
//		appG.Response(http.StatusBadRequest, "失败", "TotalArea总面积和LivingSpace生活空间必须大于0，且生活空间小于等于总面积")
//		return
//	}
//
//	var bodyBytes [][]byte
//	bodyBytes = append(bodyBytes, []byte(body.AccountId))
//	bodyBytes = append(bodyBytes, []byte(body.Proprietor))
//	bodyBytes = append(bodyBytes, []byte(body.HouseName))
//	bodyBytes = append(bodyBytes, []byte(strconv.FormatFloat(body.TotalArea, 'E', -1, 64)))
//	bodyBytes = append(bodyBytes, []byte(strconv.FormatFloat(body.LivingSpace, 'E', -1, 64)))
//	bodyBytes = append(bodyBytes, []byte(strconv.FormatFloat(body.HouseRent, 'E', -1, 64)))
//	bodyBytes = append(bodyBytes, []byte(Upload))
//	bodyBytes = append(bodyBytes, []byte(body.HouseComment))
//	//调用智能合约
//	resp, err := bc.ChannelExecute("createRealEstate", bodyBytes)
//	if err != nil {
//		appG.Response(http.StatusInternalServerError, "失败", err.Error())
//		return
//	}
//	var data map[string]interface{}
//	if err = json.Unmarshal(bytes.NewBuffer(resp.Payload).Bytes(), &data); err != nil {
//		appG.Response(http.StatusInternalServerError, "失败", err.Error())
//		return
//	}
//	appG.Response(http.StatusOK, "成功", data)
//}

func QueryRealEstateList(c *gin.Context) {
	appG := app.Gin{C: c}
	body := new(RealEstateQueryRequestBody)
	//解析Body参数
	if err := c.ShouldBind(body); err != nil {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
	var bodyBytes [][]byte
	if body.Proprietor != "" {
		bodyBytes = append(bodyBytes, []byte(body.Proprietor))
	}
	//调用智能合约
	resp, err := bc.ChannelQuery("queryRealEstateList", bodyBytes)
	if err != nil {
		appG.Response(http.StatusInternalServerError, "失败", err.Error())
		return
	}
	// 反序列化json
	var data []map[string]interface{}
	if err = json.Unmarshal(bytes.NewBuffer(resp.Payload).Bytes(), &data); err != nil {
		appG.Response(http.StatusInternalServerError, "失败", err.Error())
		return
	}
	appG.Response(http.StatusOK, "成功", data)
}
